package com.volcreate.inn.controller;

import com.github.pagehelper.PageInfo;
import com.volcreate.inn.po.Tag;
import com.volcreate.inn.service.impl.BlogService;
import com.volcreate.inn.service.impl.TagService;
import com.volcreate.inn.service.impl.TypeService;
import com.volcreate.inn.vo.BlogQuery;
import com.volcreate.inn.vo.TagVO;
import com.volcreate.inn.vo.TypeVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author zxc
 * @version 0.0.1
 * @date 2020/8/22
 */

@Controller
public class SubtotalController {


    private BlogService blogService;
    private TypeService typeService;
    private TagService tagService;

    public SubtotalController(BlogService blogService, TypeService typeService, TagService tagService) {
        this.blogService = blogService;
        this.typeService = typeService;
        this.tagService = tagService;
    }

    @GetMapping("/fast/type")
    public String typeRedirect(){
        return "redirect:/fast";
    }
    @GetMapping("/fast/tag")
    public String tagRedirect(){
        return "redirect:/fast";
    }

    @GetMapping("/fast")
    public String category(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           Model model) {
        PageInfo<TagVO> tags = tagService.listByBlogSizeDesc(1, 10000);
        PageInfo<TypeVO> types = typeService.listByBlogSizeDesc(1, 10000);
        // 先列出全部的blog
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSortBy("views");
        model.addAttribute("page", blogService.listByBlogQuery(page, 4, blogQuery));
        model.addAttribute("tags", tags);
        model.addAttribute("types", types);
        model.addAttribute("activeTopId", 1);
        return "category";
    }

    @PostMapping("/fast")
    public String categoryPost(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                               Model model) {
        // 先列出全部的blog
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSortBy("views");
        model.addAttribute("page", blogService.listByBlogQuery(page, 4, blogQuery));
        return "category :: blogList";
    }

    // 根据点击的标签来跳转types和tags
    @GetMapping("/fast/type/{id}")
    public String type(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       @PathVariable Long id, Model model) {
        PageInfo<TagVO> tags = tagService.listByBlogSizeDesc(1, 10000);
        PageInfo<TypeVO> types = typeService.listByBlogSizeDesc(1, 10000);
        // 根据id查找blog
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSortBy("views");
        blogQuery.setTypeId(id);
        model.addAttribute("page", blogService.listByBlogQuery(page, 4, blogQuery));
        model.addAttribute("tags", tags);
        model.addAttribute("types", types);
        model.addAttribute("activeTypeId", id);
        model.addAttribute("activeTopId", 1);
        return "category";
    }

    @GetMapping("/fast/tag/{id}")
    public String tag(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                      @PathVariable Long id, Model model) {
        PageInfo<TagVO> tags = tagService.listByBlogSizeDesc(1, 10000);
        PageInfo<TypeVO> types = typeService.listByBlogSizeDesc(1, 10000);
        BlogQuery blogQuery = new BlogQuery();
        Tag tag = new Tag();
        tag.setId(id);
        blogQuery.setTag(tag);
        blogQuery.setSortBy("views");
        model.addAttribute("page", blogService.listByBlogQuery(page, 4, blogQuery));
        model.addAttribute("tags", tags);
        model.addAttribute("types", types);
        model.addAttribute("activeTagId", id);
        model.addAttribute("activeTopId", 2);
        return "category";
    }

    @PostMapping("/fast/tag/{id}")
    public String tagPost(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                          @PathVariable Long id, Model model) {

        BlogQuery blogQuery = new BlogQuery();
        Tag tag = new Tag();
        tag.setId(id);
        blogQuery.setTag(tag);
        model.addAttribute("page", blogService.listByBlogQuery(page, 4, blogQuery));
        model.addAttribute("activeTagId", id);
        return "category  :: blogList";
    }

    // 根据点击的标签来跳转types和tags
    @PostMapping("/fast/type/{id}")
    public String typePost(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           @PathVariable Long id, Model model) {
        // 根据id查找blog
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        blogQuery.setSortBy("views");
        model.addAttribute("page", blogService.listByBlogQuery(page, 4, blogQuery));
        model.addAttribute("activeTypeId", id);
        return "category :: blogList";
    }


}
